﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics;
using GurLoader.Util;

namespace GurLoader.Service
{
    public class DelegatingCommandProgress : CommandProgressEx, IDelegatingCommandProgress
    {
        private static readonly PropertyChangedEventArgs totalProgressPercentageChangeArgs =
            ObservableHelper.CreateArgs<DelegatingCommandProgress>(x => x.TotalProgressPercentage);

        private static readonly PropertyChangedEventArgs totalProgressCompleteChangeArgs =
            ObservableHelper.CreateArgs<DelegatingCommandProgress>(x => x.IsTotalProgressComplete);

        private readonly object _lock = new object();
        private bool isTotalProgressComplete = false;

        public DelegatingCommandProgress(IDispatcher dispatcher) : base(Command.Unknown, int.MaxValue, string.Empty, new List<CommandProgress>(), dispatcher )
        {
            
        }

        public DelegatingCommandProgress(Command command, int progressPercentage, string status,
            List<CommandProgress> children, IDispatcher dispatcher) : base(command, progressPercentage, status, children, dispatcher)
        {
            Children.CollectionChanged += OnChildrenCollectionChanged;
            foreach (CommandProgress child in Children)
            {
                child.PropertyChanged += OnChildPropertyChanged;
            }
            PropertyChanged += OnPropertyChanged;
        }

        private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName.Equals("TotalProgressPercentage"))
            {
                if (TotalProgressPercentage == 100)
                    IsTotalProgressComplete = true;
            }
        }

        public DelegatingCommandProgress(Command command, int progressPercentage, string status, IDispatcher dispatcher)
            : base(command, progressPercentage, status, new List<CommandProgress>(), dispatcher)
        {
            Children.CollectionChanged += OnChildrenCollectionChanged;
            PropertyChanged += OnPropertyChanged;
        }

        public int TotalProgressPercentage
        {
            get
            {
                int percentComplete = 0;
                lock (_lock)
                {
                    int countComplete = 0;

                    foreach (CommandProgress child in Children)
                    {
                        if (child.IsComplete)
                            countComplete++;
                    }

                    double totalTasks = Children.Count;
                    double numberCompleteTasks = countComplete;

                    percentComplete = (int) ((numberCompleteTasks/totalTasks)*100);
                }

                return percentComplete;
            }

            set { throw new InvalidOperationException(); }
        }

        private void OnChildPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName.Equals("ProgressPercentage"))
                NotifyPropertyChanged(totalProgressPercentageChangeArgs);
        }

        private void OnChildrenCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            switch (e.Action)
            {
                case NotifyCollectionChangedAction.Add:
                    foreach (CommandProgress item in e.NewItems)
                    {
                        item.PropertyChanged += OnChildPropertyChanged;
                    }
                    break;
                case NotifyCollectionChangedAction.Remove:
                    foreach (CommandProgress item in e.OldItems)
                    {
                        item.PropertyChanged -= OnChildPropertyChanged;
                    }
                    break;
            }
        }

        public bool IsTotalProgressComplete
        {
            get { return isTotalProgressComplete; }
            set
            {
                if (isTotalProgressComplete == value)
                    return;

                isTotalProgressComplete = value;
                NotifyPropertyChanged(totalProgressCompleteChangeArgs);
            }
        }

        public override bool IsComplete
        {
            get { return IsTotalProgressComplete; }
        }
    }
}